package com.ruoyi.framework.aspectj;


import com.ruoyi.api.domain.UserEntity;
import com.ruoyi.common.annotation.RequiresLogin;
import com.ruoyi.common.core.redis.RedisCache;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Aspect
@Component
public class LoginAspect {

//    private final HttpServletRequest request;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private HttpServletRequest request;
//    public LoginAspect(HttpServletRequest request) {
//        this.request = request;
//    }

    @Before("@annotation(requiresLogin)")
    public void checkLogin(RequiresLogin requiresLogin) throws Throwable {
        // 获取用户的登录信息（例如，检查请求头或会话中的用户信息）
       String token =  request.getHeader("Authorization");
        Map<String, UserEntity> map = redisCache.getCacheMap("token");
        if (!map.containsKey(token)) {
//            throw new RuntimeException("用户未登录，请登录后重试！");
        }else{
            UserEntity user = map.get(token);
            request.setAttribute("user",user);
        }

    }
}
